package mall;

import mall.common.enums.PromotionTypeEnum;
import mall.pojo.ProductDO;
import mall.promotion.PromotionManager;
import mall.promotion.PromotionStrategy;
import mall.promotion.impl.DiscountPromotion;
import mall.promotion.impl.FullReductionPromotion;
import mall.service.CheckoutService;
import mall.service.ShelfService;
import mall.service.ShopCarService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 超市
 * @author zmy
 * @since 2025/6/27 23:35
 */
public class MainMall {
    private final Scanner sc = new Scanner(System.in);
    /** 商品货架 */
    private final ShelfService shelfService = ShelfService.getInstance();
    /** 购物车 */
    private ShopCarService shopCarService;

    /** 促销活动管理器 */
    private final PromotionManager promotionManager = new PromotionManager();
    /** 促销校验 */
    private final CheckoutService  checkoutService= new CheckoutService(promotionManager);

    public MainMall() {
        // 初始化商品
        ProductDO apple = new ProductDO("10001", "apple", 8D, 999);
        ProductDO strawberry = new ProductDO("10002", "strawberry", 13D, 999);
        this.shelfService.addGood(apple);
        this.shelfService.addGood(strawberry);
        this.shopCarService = ShopCarService.getInstance(shelfService);
    }

    public static void main(String[] args) {
        new MainMall().startWork();
    }

    /** 超市开业 */
    public void startWork() {
        int choice = 0;
        String productId = null;
        int count;
        String promotionType = null;

        do {
            showMenu();
            System.out.print("请输入要进行的操作：");
            choice = inputInteger();
            switch (choice) {
                case 1:
                    shelfService.showGoods();
                    break;
                case 2:
                    ProductDO addTarget = inputProduct();
                    shelfService.addGood(addTarget);
                    break;
                case 3:
                    System.out.print("请输入要下架的商品ID：");
                    String delId = inputStr();
                    shelfService.deleteProductById(delId);
                    break;
                case 4:
                    System.out.print("请输入要增加库存的商品ID：");
                    productId = inputStr();
                    System.out.print("请输入要增加的库存数量：");
                    count = inputInteger();
                    shelfService.addQuantity(productId, count);
                    break;
                case 5:
                    promotionManager.showPromotions();
                    break;
                case 6:
                    PromotionStrategy promotionStrategy = addPromotion();
                    promotionManager.addStrategy(promotionStrategy);
                    System.out.println("添加活动成功！\n");
                    break;
                case 7:
                    break;
                case 8:
                    break;
                case 9:
                    System.out.print("请输入要购买的商品id：");
                    productId = inputStr();
                    System.out.print("请输入要购买的商品数量：");
                    count = inputInteger();
                    shopCarService.addShop(productId, count);
                    break;
                case 10:
                    shopCarService.showCar();
                    break;
                case 11:
                    checkoutService.checkout(shopCarService.showCar());
                    break;
                case 0:
                    System.out.println("谢谢惠顾！");
                    break;
                default:
                    System.out.println("选择不存在，请重新选择...");
                    break;
            }
        } while (choice != 0);
    }

    private void showMenu() {
        System.out.println("1.查看货架商品");
        System.out.println("2.新增商品");
        System.out.println("3.删除商品");
        System.out.println("4.增加商品库存");
        System.out.println("5.查看促销活动");
        System.out.println("6.新增促销活动");
        System.out.println("7.禁用促销活动");
        System.out.println("8.开启促销活动");
        System.out.println("9.购物");
        System.out.println("10.查看购物车");
        System.out.println("11.结账");
        System.out.println("0.离开");
    }

    /** 新增活动 */
    public PromotionStrategy addPromotion () {
        System.out.println("1, 折扣");
        System.out.println("2, 满减");
        System.out.print("选择要新增的类型：");
        int promotionType = inputInteger();
        PromotionStrategy promotionStrategy = null;

        if (promotionType == 1) {
            promotionStrategy = inputDiscountPromotion();
        } else if (promotionType == 2) {
            promotionStrategy = inputFullReductionPromotion();
        } else {
            System.out.println("输入错误！\n");
        }

        return promotionStrategy;
    }


    /** 一行输入一个整数 */
    private int inputInteger() {
        return Integer.parseInt(sc.nextLine().trim());
    }
    /** 一行输入一个浮点数 */
    private double inputDouble() {
        return Double.parseDouble(sc.nextLine().trim());
    }
    /** 一行输入一个字符串 */
    private String inputStr() {
        return sc.nextLine().trim();
    }

    /** 输入商品信息 */
    private ProductDO inputProduct() {
        System.out.print("商品ID：");
        String id = inputStr();
        System.out.print("商品名字：");
        String name = inputStr();
        System.out.print("商品价格：");
        double price = inputDouble();
        System.out.print("商品数量：");
        int quantity = inputInteger();
        return new ProductDO(id, name, price, quantity);
    }

    /** 输入折扣活动 */
    private DiscountPromotion inputDiscountPromotion() {
        System.out.print("请输入折扣活动的ID：");
        String id = inputStr();
        System.out.print("请输入折扣活动的名称：");
        String name = inputStr();
        System.out.print("请输入折扣活动的范围(" + PromotionTypeEnum.showAll() +")：");
        String promotionType = inputStr();
        System.out.print("请输入折扣活动要应用的商品ID(多个用英文逗号隔开，没有可不写)：");
        String[] ids = inputStr().split(",");
        Set<String> idList = Arrays.stream(ids).collect(Collectors.toSet());
        System.out.print("请输入折扣活动的优先级：");
        int priority = inputInteger();
        System.out.print("请输入折扣力度（范围0-1，例如：0.8打八折）：");
        Double discountRate = inputDouble();
        if (discountRate < 0 || discountRate > 1) {
            System.out.println("折扣率输入错误");
        }

        DiscountPromotion discountPromotion = new DiscountPromotion(id, name, promotionType, idList, true, priority, discountRate);
        return discountPromotion;
    }
    /** 输入满减活动 */
    private FullReductionPromotion inputFullReductionPromotion() {
        System.out.print("请输入活动的ID：");
        String id = inputStr();
        System.out.print("请输入活动的名称：");
        String name = inputStr();
        System.out.print("请输入活动的范围(" + PromotionTypeEnum.showAll() +")：");
        String promotionType = inputStr();
        System.out.print("请输入活动要应用的商品ID(多个用英文逗号隔开，没有可不写)：");
        String[] ids = inputStr().split(",");
        Set<String> idList = Arrays.stream(ids).collect(Collectors.toSet());
        System.out.print("请输入折扣活动的优先级：");
        int priority = inputInteger();
        System.out.print("请输入满减阈值：");
        double threshold = inputDouble();
        System.out.print("请输入满减的力度：：");
        double reduction = inputDouble();

        return new FullReductionPromotion(id, name, promotionType, idList, true, priority, threshold, reduction);
    }


    public ShelfService getShelfService() {
        return shelfService;
    }

    public ShopCarService getShopCarService() {
        return shopCarService;
    }

    public PromotionManager getPromotionManager() {
        return promotionManager;
    }

    public CheckoutService getCheckoutService() {
        return checkoutService;
    }
}
